/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.commons.speeder.monitor.handler.impl;

import cn.ac.iie.di.commons.speeder.monitor.handler.StatHandler;
import java.util.Map;

/**
 *
 * @author zwlin
 */
public class SysOutStatHandler<T> implements StatHandler<T> {

    private static final String DEFAULT_FORMAT = "Monitering Log - ";

    public static enum Type {
        TOGETHER, SEPERATELY;
    }

    public static enum Level {
        OUT, ERR;
    }
    private final Type type;
    private final Level level;
    private String formatStr;

    public SysOutStatHandler() {
        this(DEFAULT_FORMAT, Type.SEPERATELY, Level.OUT);
    }

    public SysOutStatHandler(String formatStr) {
        this(formatStr, SysOutStatHandler.Type.SEPERATELY, SysOutStatHandler.Level.OUT);
    }

    public SysOutStatHandler(String formatStr, Type type, Level level) {
        this.type = type;
        this.level = level;
        this.formatStr = formatStr;
    }

    @Override
    public void accept(Map<String, T> statMap) {
        switch (type) {
            case TOGETHER:
                print(statMap.toString());
                return;
            case SEPERATELY:
                statMap.forEach((k, v) -> print(k, v));
                return;
        }
    }

    private void print(Object... obj) {
        switch (level) {
            case OUT:
                System.out.printf(getFormatStr(), obj);
                return;
            case ERR:
                System.err.printf(getFormatStr(), obj);
                return;
        }
    }

    public String getFormatStr() {
        return formatStr;
    }

    public void setFormatStr(String formatStr) {
        this.formatStr = formatStr;
    }
}
